#encoding=utf-8

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import codecs
import MySQLdb
import MySQLdb.cursors
import json
from datetime import datetime
from twisted.enterprise import adbapi

import sys
reload(sys)
sys.setdefaultencoding('utf8')

class MaoyanmoviePipeline(object): 

    def __init__(self):
        self.file = codecs.open('maoyanmovie.json', 'w', encoding='utf-8')

    def process_item(self, item, spider):
        line = json.dumps(dict(item), ensure_ascii = False).replace('\\n',' ')
        self.file.write(line)
        return item

class MysqlPipeline(object):

    def __init__(self, dbpool):
        self.dbpool = dbpool

    @classmethod
    def from_settings(cls, settings):
        dbargs = dict(
            host = settings['MYSQL_HOST'],
            db = settings['MYSQL_DBNAME'],  
            user = settings['MYSQL_USER'],
            passwd = settings['MYSQL_PASSWD'],
            charset = 'utf8',
            cursorclass = MySQLdb.cursors.DictCursor,
            use_unicode = True,
        )
        dbpool = adbapi.ConnectionPool('MySQLdb', **dbargs)
        return cls(dbpool)
    
    def process_item(self, item, spider):
        res = self.dbpool.runInteraction(self.insert_into_table, item)
        return res
  
    def insert_into_table(self, conn, item):
        now = datetime.now()
        conn.execute("insert into maoyanmovie(name, genres, dur, country, release, score, descr, create_time) values (%s, %s, %s, %s, %s, %s, %s, %s)", (
            item['name'], 
            item['genres'],
            item['dur'],
            item['country'],
            item['release'], 
            item['score'], 
            item['descr'], 
            now
            )
        )
           
       
    


